Utils.Chrome = {};
Utils.Chrome.contextMenu = {};
Utils.Chrome.contextMenu.ids = {}
Utils.Chrome.contextMenu.init = function()
{
    if(chrome.extension.contextMenu instanceof Object)
    {
        chrome.extension.contextMenu = chrome.extension.contextMenu;
    }
    else if(chrome.experimental.contextMenu instanceof Object)
    {
        chrome.extension.contextMenu = chrome.experimental.contextMenu;
    }
}
Utils.Chrome.contextMenu.KillAll = function()
{

    var ids = Utils.Chrome.contextMenu.ids;
    var id;
    for(i in ids)
    {
        id = ids[i];
        chrome.extension.contextMenu.remove(id);
        delete id;
    }
}
Utils.Chrome.contextMenu.Build = function()
{ //Utils.Chrome.contextMenu.Build()

    Utils.Chrome.contextMenu.init();
    Utils.Chrome.contextMenu.KillAll();

    // Share this page
    chrome.extension.contextMenu.create({
            type: 'NORMAL',
            title: Traduz('share_this_page'),
            contexts: ['PAGE'],
            onclick: function(o, tab)
            {
                Utils.Short.Back(tab.url, function(shorted)
                {
                    var txt = tab.title+': '+shorted;

                    if(Utils.Chrome.infoBar.init())
                    {
                        var _t = escape(txt);
                        Utils.Chrome.infoBar.show('infobar/tweet.html#'+_t);
                    }
                    else
                    {
                        if(prompt('Chrowety - '+Traduz('share_this_page'), txt))
                        {
                            Chrowety.update(txt, null, 'home', function(OK, tid)
                            {
                                if(OK)
                                {
                                    Utils.Notify.Individual(tid, 'home');
                                }
                                else
                                {
                                    alert('Chrowety: \n'+Traduz('error')+', '+Traduz('try_again'));
                                }
                            });
                        }
                    }

                });
            }
    },function(tid){
        Utils.Chrome.contextMenu.ids['share_this_page'] = tid;
    });

    // Quick Tweet
    chrome.extension.contextMenu.create({
            type: 'NORMAL',
            title: Traduz('quick_tweet'),
            contexts: ['PAGE'],
            onclick: function(o, tab)
            {
                if(Utils.Chrome.infoBar.init())
                {
                    Utils.Chrome.infoBar.show('infobar/tweet.html');
                }
                else
                {
                    var txt = prompt('Chrowety - '+Traduz('quick_tweet'), db.pega('last_quick_tweet'));
                    if(txt)
                    {
                        Chrowety.update(txt, null, 'home', function(OK, tid)
                        {
                            if(OK)
                            {
                                Utils.Notify.Individual(tid, 'home');
                                db.deleta('last_quick_tweet');
                            }
                            else
                            {
                                db.salva('last_quick_tweet', txt);
                                alert('Chrowety: \n'+Traduz('error')+', '+Traduz('try_again'));
                            }
                        });
                    }
                }

            }
    },function(tid){
        Utils.Chrome.contextMenu.ids['quick_tweet'] = tid;
    });

    // Selection Quick Tweet
    chrome.extension.contextMenu.create({
            type: 'NORMAL',
            title: 'Chrowety '+Traduz('quick_tweet')+": '%s'",
            contexts: ['SELECTION'],
            onclick: function(info, tab)
            {
                if(Utils.Chrome.infoBar.init())
                {
                    var _t = escape(info.selectionText);
                    Utils.Chrome.infoBar.show('infobar/tweet.html#'+_t);
                }
                else
                {
                    var txt = prompt('Chrowety - '+Traduz('quick_tweet'), info.selectionText);
                    if(txt)
                    {
                        Chrowety.update(txt, null, 'home', function(OK, tid)
                        {
                            if(OK)
                            {
                                Utils.Notify.Individual(tid, 'home');
                                db.deleta('last_quick_tweet');
                            }
                            else
                            {
                                db.salva('last_quick_tweet', txt);
                                alert('Chrowety: \n'+Traduz('error')+', '+Traduz('try_again'));
                            }
                        });
                    }
                }

            }
    },function(tid){
        Utils.Chrome.contextMenu.ids['selection_quick_tweet'] = tid;
    });

    // Tweet this Image
    chrome.extension.contextMenu.create({
            type: 'NORMAL',
            title: 'Chrowety: '+Traduz('tweet_this_image'),
            contexts: ['IMAGE'],
            onclick: function(info, tab)
            {
                if(Utils.Chrome.infoBar.init())
                {
                    var _t = escape(info.srcUrl);
                    Utils.Chrome.infoBar.show('infobar/tweet.html#'+_t);
                }
                else
                {
                    var txt = prompt('Chrowety - '+Traduz('tweet_this_image'), info.srcUrl);
                    if(txt)
                    {
                        Chrowety.update(txt, null, 'home', function(OK, tid)
                        {
                            if(OK)
                            {
                                Utils.Notify.Individual(tid, 'home');
                                db.deleta('last_quick_tweet');
                            }
                            else
                            {
                                db.salva('last_quick_tweet', txt);
                                alert('Chrowety: \n'+Traduz('error')+', '+Traduz('try_again'));
                            }
                        });
                    }
                }

            }
    },function(tid){
        Utils.Chrome.contextMenu.ids['tweet_this_image'] = tid;
    });
    /*
    // Configuration
    chrome.extension.contextMenu.create({
            type: 'NORMAL',
            title: Traduz('configuration'),
            contexts: ['PAGE']
    },function(id){
        console.log(id);
        chrome.extension.contextMenu.create({
                type: 'CHECKBOX',
                checked: true,
                title: Traduz('desktop_alerts'),
                parentId: id
        });
    });
    */
}

Utils.Chrome.i18n = {};
Utils.Chrome.i18n.get = function(string, subs)
{
    return chrome.i18n.getMessage(string, subs);
}

Utils.Chrome.i18n.addLang = function(lang)
{
    chrome.i18n.appendAcceptLanguage(lang);
}

Utils.Chrome.infoBar = {};
Utils.Chrome.infoBar.init = function()
{
    if(chrome.extension.infobars  instanceof Object)
    {
        chrome.extension.infobars = chrome.extension.infobars;
        return true;
    }
    else if(chrome.experimental.infobars instanceof Object)
    {
        chrome.extension.infobars = chrome.experimental.infobars;
        return true;
    }
    else
    {
        return false;
    }
}
Utils.Chrome.infoBar.show = function(url, callback)
{
    Utils.Chrome.infoBar.init();
    var callback = callback instanceof Function  ? callback :
    function(w)
    {
        console.log(w);
    };
    chrome.extension.infobars.show({ 'path': url }, callback);
}

Utils.Chrome.clipBoard = {};
Utils.Chrome.clipBoard.copy = function()
{
    Utils.Chrome.tabs.getCurrent(function(tab){
        chrome.experimental.clipboard.executeCopy(tab.id);
    });
}

Utils.Chrome.tabs = {};
Utils.Chrome.tabs.getCurrent = function(callback)
{
    chrome.windows.getAll({}, function(w){
        chrome.tabs.getSelected(w[0].id, callback);
    });
}

Utils.Chrome.browserAction = {};
Utils.Chrome.browserAction.text = function(txt)
{
    chrome.browserAction.setBadgeText({text:''+ (txt?txt:'') });
}

Utils.Chrome.browserAction.title = function(tit)
{
    chrome.browserAction.setTitle({title: (tit?tit:'') });
}

Utils.Chrome.browserAction.color = function(cor)
{
    chrome.browserAction.setBadgeBackgroundColor({color: (cor?cor:[255, 255, 255, 255]) });
}
Utils.Chrome.views = {};
Utils.Chrome.views = {};
Utils.Chrome.views.popup = function()
{
    return chrome.experimental.extension.getPopupView();
}
//Utils.Chrome.views.tab =
